﻿/*
 * Id=-1 回收站(已停用)
 * ParenGroupId=0 根节点
 * 已有账务的群组无法更改收支类型
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FCNS.Utility;

using FCNS.KeepMoney.UI;
using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoney.Util;

namespace FCNS.KeepMoney.Forms
{
    public partial class GroupForm : ParentForm
    {
        public GroupForm()
        {
            InitializeComponent();
            Program.Translation.ApplyTo(this);
        }

        GroupField groupField = null;

        public void Init(GroupField field)
        {
            BannerFactory.CreateBannerEx(this, pictureBoxLogo, Properties.Resources.folder32,
               LanguageUtil.编辑群组, LanguageUtil.编辑当前选定群组的属性);

            comboBoxLevel.Items.Add(LanguageUtil.顶层);
            comboBoxLevel.Items.AddRange(GroupField.GetGroupNames(GroupField.GetGroupField));
            comboBoxLevel.SelectedIndex = 0;

            if (field == null)
            {
                buttonIcon.Tag = 0;
                buttonIcon.BackgroundImage = Program.ImagesList.Images[0];
                groupField = new GroupField(0, string.Empty, true);
            }
            else
            {
                groupField = field;
                textBoxName.Text = field.Name;
                buttonIcon.BackgroundImage = Program.ImagesList.Images[field.IconIndex];
                buttonIcon.Tag = field.IconIndex;
                checkBoxIO.Checked = field.IorE;
                textBoxNote.Text = field.Note;
                comboBoxLevel.SelectedIndex = field.ParentGroupId == 0 ? 0 :
                    GroupField.GetGroupField.Find(gf => { return gf.Id == field.ParentGroupId; }).Index + 1;

                if (EntryField.GetEntryField.Find(ef => { return ef.GroupId == field.Id; }) != null)
                    checkBoxIO.Enabled = false;
            }
        }

        private void OnChangeGroupIcon(object sender, EventArgs e)
        {
            IconListForm i = new IconListForm();
            if (i.ShowDialog() == DialogResult.OK)
            {
                buttonIcon.BackgroundImage = Program.ImagesList.Images[i.IconIndex];
                buttonIcon.Tag = i.IconIndex;
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (textBoxName.Text.Trim() == "")
            {
                this.DialogResult = DialogResult.Cancel;
                return;
            }

            groupField.Name = textBoxName.Text;
            groupField.IconIndex = (int)buttonIcon.Tag;
            groupField.IorE = checkBoxIO.Checked;
            groupField.Note = textBoxNote.Text;

            int newParentGroupId = 0;
            if (comboBoxLevel.SelectedIndex != 0)
            {
                GroupField group = GroupField.GetGroupField.Find(gf => { return gf.Index == comboBoxLevel.SelectedIndex - 1; });
                if (group.Index != groupField.Index)
                    newParentGroupId = group.Id;
                else
                    newParentGroupId = groupField.ParentGroupId;
            }

            if (groupField.SearchAllSubField(newParentGroupId))
                groupField.ChangeParentGroup(newParentGroupId);
            else
                groupField.ParentGroupId = newParentGroupId;

            groupField.AddNewRowToDataTable();

            Program.TheMainForm.UpdateUI(UIState.DataHasBeenChanged);
            this.DialogResult = DialogResult.OK;
        }
    }
}